package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.Finish1;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.CircularImage;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

public class GroupPersonInfoActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_person_info);
        initTitle();
        initData();
        initView();
    }

    private TextView tv_user_id, tv_telephone, tv_user_name, tv_user_img, tv_is_mine, tv_classname;
    private TextView send_msg;
    private CircularImage head_img;
    private TextView my_group, how_group;
    private TextView shengji, jiangji, tuichu;
    private int is_manager = -1;

    private void initView() {
        tv_telephone = (TextView) findViewById(R.id.telephone);
        tv_telephone.setOnClickListener(this);
        tv_user_name = (TextView) findViewById(R.id.user_name);
        tv_classname = (TextView) findViewById(R.id.classname);
        send_msg = (TextView) findViewById(R.id.send_msg);
        send_msg.setOnClickListener(this);
        head_img = (CircularImage) findViewById(R.id.head_img);
        my_group = (TextView) findViewById(R.id.my_group);
        how_group = (TextView) findViewById(R.id.how_group);
        shengji = (TextView) findViewById(R.id.shegnji);
        jiangji = (TextView) findViewById(R.id.jiangji);
        tuichu = (TextView) findViewById(R.id.tuichu);
        tuichu.setOnClickListener(this);
        findViewById(R.id.goto_order).setOnClickListener(this);
        findViewById(R.id.goto_recode).setOnClickListener(this);
        findViewById(R.id.goto_task).setOnClickListener(this);
        findViewById(R.id.goto_plan).setOnClickListener(this);
        setData();
    }

    private void setData() {
        tv_telephone.setText(telephone);
        tv_user_name.setText(user_name);
        tv_classname.setText(classname);
//        x.image().bind(head_img, user_img);
        Glide.with(GroupPersonInfoActivity.this).load(user_img).into(head_img);
        my_group.setText(mygroup);
        how_group.setText(howgroup);
        if (is_manager == 1) {
            if ("1".equals(status) && "0".equals(is_mine)) {
                shengji.setOnClickListener(this);
                shengji.setVisibility(View.VISIBLE);
            } else if ("0".equals(status) && "0".equals(is_mine)) {
                jiangji.setOnClickListener(this);
                jiangji.setVisibility(View.VISIBLE);
            }
        }
        if ("1".equals(is_mine)) {
            tuichu.setVisibility(View.VISIBLE);
        }
    }

    private String user_id, telephone, user_name, user_img, is_mine, classname, mygroup, howgroup;
    private String status;
    private String team_id;

    private void initData() {
        user_id = getIntent().getStringExtra("user_id");
        telephone = getIntent().getStringExtra("telephone");
        user_name = getIntent().getStringExtra("user_name");
        user_img = getIntent().getStringExtra("user_img");
        is_mine = getIntent().getStringExtra("is_mine");
        classname = getIntent().getStringExtra("classname");
        mygroup = getIntent().getStringExtra("mygroup");
        howgroup = getIntent().getStringExtra("howgroup");
        status = getIntent().getStringExtra("status");
        team_id = getIntent().getStringExtra("team_id");
        is_manager = getIntent().getIntExtra("is_manager", -1);
    }


    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("人员详情");
        //title.setTextSize(20);
        title.setTextColor(getResources().getColor(R.color.main_color));
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setText("邀请");
        submit.setTextColor(getResources().getColor(R.color.main_color));
        submit.setOnClickListener(this);
        submit.setVisibility(View.GONE);

    }


    @Override
    public void onClick(View v) {
        Intent intent;
        Log.e("----", user_id + "");
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.submit:
//                intent = new Intent(this, GroupInviteActivity.class);
//                startActivity(intent);
                break;
            case R.id.telephone:
                intent = new Intent(Intent.ACTION_DIAL);
                Uri data = Uri.parse("tel:" + telephone);
                intent.setData(data);
                startActivity(intent);
                break;
            case R.id.send_msg:
                Uri smsToUri = Uri.parse("smsto://" + telephone);
                Intent mIntent = new Intent(android.content.Intent.ACTION_SENDTO, smsToUri);
                mIntent.putExtra("sms_body", "");
                startActivity(mIntent);
                break;
            case R.id.shegnji:
                gotoClass(1);
                break;
            case R.id.jiangji:
                gotoClass(2);
                break;
            case R.id.tuichu:
                gotoClass(3);
                break;
            //下单
            case R.id.goto_order:
                intent = new Intent(this, AllProject2Activity.class);
                intent.putExtra("user_id", user_id);
                startActivity(intent);
                break;
            //备案
            case R.id.goto_recode:
                intent = new Intent(this, OtherRecode2Activity.class);
                intent.putExtra("user_id", user_id);
                startActivity(intent);
                break;
            //任务
            case R.id.goto_task:
                intent = new Intent(this, TaskAllActivity.class);
                intent.putExtra("type", 2);
                intent.putExtra("title", "" + user_name + "的任务");
                intent.putExtra("user_id", user_id);
                startActivity(intent);
                break;
            //计划
            case R.id.goto_plan:
                intent = new Intent(this, Plans2Activity.class);
                intent.putExtra("user_id", user_id);
                startActivity(intent);
                break;

        }
    }

    private void gotoDoThing() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post("", map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }


    private void gotoClass(final int b) {
        String url = "";
        switch (b) {
            case 1:
                url = Url.TEAM_UPLEVELUSER;
                break;
            case 2:
                url = Url.TEAM_DOWNLEVELUSER;
                break;
            case 3:
                url = Url.TEAM_EXITTEAMBYSELF;
                break;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("team_id", team_id);
        map.put("user_id", user_id);
        showDialog();
      /*  Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }*/
        XUtil.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        if (b == 3) {
                            // MyApplication.getInstance().destoryActivity("groupDetails");
                            EventBus.getDefault().post(new Finish1());
                        }
                        finish();

                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }
}
